import scrapy

class anjuke(scrapy.Spider):
    name = "anjuke"
    city = "深圳"
    area = "shenzhenzhoubian"
    def start_requests(self):
        urls = ["https://shenzhen.anjuke.com/sale/shenzhenzhoubian/"]

        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        all_house = response.xpath('//li[@class="list-item"]')
        next_url = response.xpath("//div[@class='multi-page']/a[@class='aNxt']/@href")
        for one_house in all_house:
            # title = one_house.xpath('.//div[@class="house-title"]/a/text()').extract()[0]
            # house = response.xpath('.//div[@class="details-item"][1]/span[1]/text()').extract()[0]
            # house_big = response.xpath('.//div[@class="details-item"][1]/span[2]/text()').extract()[0]
            # house_build_year = response.xpath('.//div[@class="details-item"][1]/span[4]/text()').extract()[0]
            # house_location = response.xpath('.//div[@class="details-item"][2]/span/text()').extract()[0]
            # total_price = response.xpath('.//div[@class="pro-price"]/span/strong/text()').extract()[0]+"万"
            # unit_price = response.xpath('.//div[@class="pro-price"]/span[2]/text()').extract()[0]
            # print(one_house.xpath('.//div[@class="house-title"]/a/text()').extract()[0])
            Ietm_one = {
                "title" : one_house.xpath('.//div[@class="house-title"]/a/text()').extract()[0],
                "house" : one_house.xpath('.//div[@class="details-item"][1]/span[1]/text()').extract()[0],
                "house_big" : one_house.xpath('.//div[@class="details-item"][1]/span[2]/text()').extract()[0],
                "house_build_year" : one_house.xpath('.//div[@class="details-item"][1]/span[4]/text()').extract()[0],
                "house_location" : one_house.xpath('.//div[@class="details-item"][2]/span/text()').extract()[0],
                "total_price" :one_house.xpath('.//div[@class="pro-price"]/span/strong/text()').extract()[0] + "万",
                "unit_price" : one_house.xpath('.//div[@class="pro-price"]/span[2]/text()').extract()[0],
                "city" : self.city,
                "area" : self.area
            }
            #print(Ietm_one)
            yield  Ietm_one
        if next_url:
            url = next_url.extract()[0]
            yield scrapy.Request(url=url, callback=self.parse)